package com.cyt.work.wb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.wb.dao.WbCheckCodeDao;
import com.cyt.work.wb.model.po.WbCheckCode;
import com.cyt.work.wb.model.vo.WbCheckCodeVo;
import com.cyt.work.wb.service.WbCheckCodeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 网站模块-验证码service实现
 */
@Service("webCheckCodeService")
public class WbCheckCodeServiceImpl extends BaseServiceImpl<WbCheckCode> implements WbCheckCodeService {

    @Autowired
    private WbCheckCodeDao wbCheckCodeDao;

    @Autowired
    public void setBaseDao(WbCheckCodeDao wbCheckCodeDao) {
        this.baseDao = wbCheckCodeDao;
    }

    public ReturnResult page(WbCheckCodeVo wbCheckCodeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WbCheckCode> list = acceptList(wbCheckCodeVo);
        PageInfo<WbCheckCode> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    public ReturnResult addOrUpdate(WbCheckCodeVo webCheckCodeVo, SysUser currentUser) {
        boolean isUpate = true;
        WbCheckCode wbCheckCode;
        if (StrUtil.isEmpty(webCheckCodeVo.getId())) {
            wbCheckCode = new WbCheckCode(currentUser);
            isUpate = false;
        } else {
            wbCheckCode = selectByPrimaryKey(webCheckCodeVo.getId());
            if (ObjectUtil.isNull(wbCheckCode)) {
                return new ReturnResult(ErrorCode.CODE_1, ErrorCode.CODE_1_MSG);
            }
            wbCheckCode.setUpdateTime(new Date());
            wbCheckCode.setUpdateUser(currentUser.getId());
        }
        wbCheckCode.setUseStatus(webCheckCodeVo.getUseStatus());
        wbCheckCode.setPhone(webCheckCodeVo.getPhone());
        wbCheckCode.setCodeNo(webCheckCodeVo.getCodeNo());
        wbCheckCode.setEndTime(webCheckCodeVo.getEndTime());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(wbCheckCode);
        } else {
            num = insert(wbCheckCode);
        }
        if (num <= 0) {
            return new ReturnResult(ErrorCode.CODE_1, ErrorCode.CODE_1_MSG);
        }
        return new ReturnResult();
    }

    public ReturnResult delete(String id, SysUser currentUser) {
        WbCheckCode webCheckCode = selectByPrimaryKey(id);
        if (webCheckCode != null) {
            webCheckCode.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
            webCheckCode.setUpdateTime(new Date());
            webCheckCode.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(webCheckCode);
            if (num > 0) {
                return new ReturnResult();
            }
        }
        return new ReturnResult(ErrorCode.CODE_1, ErrorCode.CODE_1_MSG);
    }

    public List<WbCheckCode> acceptList(WbCheckCodeVo webCheckCodeVo) {
        Example example = new Example(WbCheckCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(webCheckCodeVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", webCheckCodeVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(webCheckCodeVo.getPhone())) {
            criteria.andEqualTo("phone", webCheckCodeVo.getPhone());
        }
        if (StringUtil.isNotEmpty(webCheckCodeVo.getCodeNo())) {
            criteria.andEqualTo("codeNo", webCheckCodeVo.getCodeNo());
        }
        if (StringUtil.isNotEmpty(webCheckCodeVo.getUseStatus())) {
            criteria.andEqualTo("useStatus", webCheckCodeVo.getUseStatus());
        }
        example.setOrderByClause(" update_time desc");
        return wbCheckCodeDao.selectByExample(example);
    }

    @Override
    public ReturnResult checkCode(String phone, String code) {
        if (StringUtils.isEmpty(phone)) {
            return new ReturnResult(ErrorCode.CODE_1, "手机号不能为空");
        }
        if (StringUtils.isEmpty(code)) {
            return new ReturnResult(ErrorCode.CODE_1, "验证码不能为空");
        }
        List<WbCheckCode> list = this.acceptList(new WbCheckCodeVo(phone, code, BaseConstant.BASE_USE_STATUS_WSY));
        if (CollectionUtil.isEmpty(list)) {
            return new ReturnResult(ErrorCode.CODE_1, "验证码错误");
        }
        WbCheckCode webCheckCodes = null;
        boolean flag = false;
        for (WbCheckCode webCheckCode : list) {
            if (System.currentTimeMillis() <= webCheckCode.getEndTime().getTime()) {
                webCheckCodes = webCheckCode;
                flag = true;
                // 只需要第一个有效的
                break;
            }
        }
        if (!flag) {
            return new ReturnResult(ErrorCode.CODE_1, "验证码已失效");
        }
        return new ReturnResult(webCheckCodes);
    }

    @Override
    public Integer selectMinuteCountByPhone(String phone) {
        return wbCheckCodeDao.selectMinuteCountByPhone(phone);
    }
}
